#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using Gizmox.WebGUI.Common.Resources;
using PLACEHR.Web.Categories.Management.Forms;
using PLACEHR.Web.Utilities;
using PLACEHR.Web.Categories.Business.Forms;

#endregion

namespace PLACEHR.Web.Categories.Business.Controls
{
    public partial class SalaryHistoryControl : UserControl, IHostedApplication
    {
        public SalaryHistoryControl()
        {
            InitializeComponent();
            InitializeData();
        }

        #region IHostedApplication

        public void InitializeApplication()
        {
        }

        public HostedToolBarElement[] GetToolBarElements()
        {
            ArrayList objElements = new ArrayList();
            objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(20), LanLayer.GetTrans(20), new IconResourceHandle("Outlook.NewItem.gif"), "Retrieve"));
            objElements.Add(new HostedToolBarSeperator());
            objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(21), LanLayer.GetTrans(21), new IconResourceHandle("CRM.ico_16_5006.gif"), "OpenSelected"));
            //objElements.Add(new HostedToolBarSeperator());
            //objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(22), LanLayer.GetTrans(22), new IconResourceHandle("CRM.ico_16_1070.gif"), "Add"));
            //objElements.Add(new HostedToolBarSeperator());
            //objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(23), LanLayer.GetTrans(23), new IconResourceHandle("CRM.ico_16_5010.gif"), "DeleteSelected"));

            return (HostedToolBarElement[])objElements.ToArray(typeof(HostedToolBarElement));
        }

        public void OnToolBarButtonClick(HostedToolBarButton objButton, EventArgs objEvent)
        {
            string strAction = (string)objButton.Tag;
            switch (strAction)
            {
                case "Retrieve":
                    InitializeData();
                    break;
                case "OpenSelected":
                    OpenDetailForm();
                    break;
                case "Add":
                    AddDetailForm();
                    break;
                case "DeleteSelected":
                    if (lvwData.CheckedItems.Count > 0)
                    {
                        MessageBox.Show("Please make sure you want to delete selected Departments!!", "Prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                        (obj, arg) =>
                                        {
                                            if (((MessageBoxWindow)obj).DialogResult == DialogResult.Yes)
                                            {
                                                DeleteSelected();
                                                InitializeData();
                                            }
                                        });
                    }
                    else
                    {
                        MessageBox.Show("Please select which catagory you want to delete!!");
                    }
                    break;
            }
        }

        #endregion

        #region Method

        private void DeleteSelected()
        {
            //foreach (ListViewItem lvwItem in lvwData.CheckedItems)
            //{
            //    Department c = lvwItem.Tag as Department;
            //    c.IsDeleted = true;
            //    //DataRow item = lvwItem.Tag as DataRow;
            //    //SqlHelper.ExecuteNonQuery(Global.ConnectionString, CommandType.Text, string.Format("delete from [Catagory] where id = {0}", item["ID"].ToString()));
            //}

            //Global.Context.SaveChanges();
        }

        private void OpenDetailForm()
        {
            if (lvwData.SelectedItem != null)
            {
                EmployeeSalary item = lvwData.SelectedItem.Tag as EmployeeSalary;
                SalaryDetailForm form = new SalaryDetailForm();
                form.currItem = item;
                form.FormClosed += new Gizmox.WebGUI.Forms.Form.FormClosedEventHandler(form_FormClosed);
                form.ShowDialog();
            }
        }

        void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            SalaryDetailForm form = sender as SalaryDetailForm;
            if (form.DialogResult == DialogResult.OK)
            {
                InitializeData();
            }
        }

        private void AddDetailForm()
        {
            SalaryDetailForm form = new SalaryDetailForm();
            form.FormClosed += new Gizmox.WebGUI.Forms.Form.FormClosedEventHandler(form_FormClosed);
            form.ShowDialog();
        }

        private void InitializeData()
        {
            lblYear.Text = LanLayer.GetTrans(152) + ":";
            lblMonth.Text = LanLayer.GetTrans(153) + ":";

            colBasicSalary.Text = LanLayer.GetTrans(154);
            colBonus.Text = LanLayer.GetTrans(155);
            colDepartment.Text = LanLayer.GetTrans(156);
            colExpatAllowance.Text = LanLayer.GetTrans(157);
            colHousingFund.Text = LanLayer.GetTrans(158);
            colIndividualTax.Text = LanLayer.GetTrans(159);
            colMonth.Text = LanLayer.GetTrans(153);
            colName.Text = LanLayer.GetTrans(160);
            colNetSalary.Text = LanLayer.GetTrans(161);
            colOffice.Text = LanLayer.GetTrans(162);
            colOthers.Text = LanLayer.GetTrans(163);
            colPaidTime.Text = LanLayer.GetTrans(164);
            colPositionSalary.Text = LanLayer.GetTrans(165);
            colSickLeave.Text = LanLayer.GetTrans(166);
            colSocialInsurance.Text = LanLayer.GetTrans(167);
            colTaxableSalary.Text = LanLayer.GetTrans(168);
            colUnPaidLeave.Text = LanLayer.GetTrans(169);
            colYear.Text = LanLayer.GetTrans(152);
            

            //List<EmployeeSalary> employeeList = Global.Context.EmployeeSalaries.Where(p => p.EmployeeID == Global.CurrentUser.EmployeeID && !p.IsDeleted).ToList();
            //this.BindDataToListView(employeeList);

            cbxYear.DataSource = Utility.GetYearList(5);
            cbxYear.DisplayMember = "value";
            cbxYear.ValueMember = "key";

            cbxYear.SelectedValue = DateTime.Now.Year.ToString();

            cbxMonth.DataSource = Utility.GetMonthList();
            cbxMonth.DisplayMember = "value";
            cbxMonth.ValueMember = "key";

            //cbxMonth.SelectedValue = DateTime.Now.Month.ToString();

        }

        private void BindDataToListView(List<EmployeeSalary> employeeList)
        {
            lvwData.Items.Clear();
            lvwData.BeginUpdate();
            int No = 1;
            foreach (var item in employeeList)
            {
                ListViewItem lvwItem = new ListViewItem("");
                lvwItem.SubItems.AddRange(new string[19]);

                lvwItem.SubItems[0].Text = (No++).ToString();
                lvwItem.SubItems[1].Text = item.Employee.EnglishName + "(" + item.Employee.ChineseName + ")";

                string office = string.Empty;
                string depart = string.Empty;
                if (item.Employee.OfficeDepartment_lnk != null && item.Employee.OfficeDepartment_lnk.Count > 0)
                {
                    foreach (var d in item.Employee.OfficeDepartment_lnk)
                    {
                        office = string.Format("{0}{1},", office, d.Office.OfficeName);
                        depart = string.Format("{0}{1},", depart, d.Department.DepartmentName);
                    }
                }
                lvwItem.SubItems[2].Text = office.Trim(new char[] { ',' });
                lvwItem.SubItems[3].Text = depart.Trim(new char[] { ',' });

                lvwItem.SubItems[4].Text = item.Year.ToString();
                lvwItem.SubItems[5].Text = item.Month.ToString();
                lvwItem.SubItems[6].Text = item.BasicSalary.ToString();
                lvwItem.SubItems[7].Text = item.PositionSalary.ToString();
                lvwItem.SubItems[8].Text = item.Bonus.ToString();
                lvwItem.SubItems[9].Text = item.UnpaidLeave.ToString();
                lvwItem.SubItems[10].Text = item.SickLeave.ToString();
                lvwItem.SubItems[11].Text = item.SocialInsuranceIndividual.ToString();
                lvwItem.SubItems[12].Text = item.HousingFundIndividual.ToString();
                lvwItem.SubItems[13].Text = item.IndividualIncomeTax.ToString();
                lvwItem.SubItems[14].Text = item.Others.ToString();
                lvwItem.SubItems[15].Text = item.TaxableSalary.ToString();
                lvwItem.SubItems[16].Text = item.ExpatAllowance.ToString();
                lvwItem.SubItems[17].Text = item.NetSalary.ToString();
                lvwItem.SubItems[18].Text = item.SalaryPaidDate.HasValue ? item.SalaryPaidDate.Value.ToString() : string.Empty;

                lvwItem.Tag = item;
                lvwData.Items.Add(lvwItem);
            }

            lvwData.EndUpdate();
        }

        #endregion

        #region Event

        private void lvwData_DoubleClick(object sender, EventArgs e)
        {
            OpenDetailForm();
        }

        private void queryControl1_FilterQueryClick(object sender, EventArgs e)
        {
            List<EmployeeSalary> salaryList = Global.Context.EmployeeSalaries.Where(p => p.EmployeeID == Global.CurrentUser.EmployeeID && !p.IsDeleted).ToList();

            if (cbxYear.SelectedValue != null && !string.IsNullOrWhiteSpace(cbxYear.SelectedValue.ToString()))
            {
                salaryList = salaryList.Where(p => p.Year.HasValue && p.Year.Value == Convert.ToInt16(cbxYear.SelectedValue)).ToList();
            }

            if (cbxMonth.SelectedValue != null && !string.IsNullOrWhiteSpace(cbxMonth.SelectedValue.ToString()))
            {
                salaryList = salaryList.Where(p => p.Month.HasValue && p.Month.Value == Convert.ToInt16(cbxMonth.SelectedValue)).ToList();
            }

            this.BindDataToListView(salaryList);
        }

        #endregion



    }
}